package com.ruoyi.flowable.controller;

import org.apache.commons.lang3.StringUtils;
import org.flowable.idm.api.Group;
import org.flowable.idm.api.GroupQuery;
import org.flowable.idm.api.IdmIdentityService;
import org.flowable.ui.common.model.GroupRepresentation;
import org.flowable.ui.common.model.ResultListDataRepresentation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * flowable modeler 用户组选择接口
 */
@RestController
public class EditorGroupsResourceController {


    protected final IdmIdentityService identityService;

    public EditorGroupsResourceController(IdmIdentityService identityService) {
        this.identityService = identityService;
    }

    @GetMapping(value = "/rest/editor-groups")
    public ResultListDataRepresentation getGroups(@RequestParam(required = false, value = "filter") String filter) {
        List<GroupRepresentation> result = new ArrayList<>();
        List<? extends Group> groups;
        GroupQuery groupQuery = identityService.createGroupQuery();
        if (StringUtils.isNotEmpty(filter)) {
            groupQuery.groupNameLikeIgnoreCase("%" + filter + "%");
        }
        groups = groupQuery.orderByGroupName().asc().list();
        for (Group group : groups) {
            result.add(new GroupRepresentation(group));
        }
        return new ResultListDataRepresentation(result);
    }

}
